package com.quanshu.exchange.ui.gui.message.friends;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 搜索好友
 * Created by jye on 2017/12/13.
 */

public class SearchUserActivity extends BaseActivity {

    @BindView(R.id.search_bar_editText)
    EditText searchBarEditText;
    @BindView(R.id.search_bar_delete)
    ImageButton searchBarDelete;
    @BindView(R.id.search_bar_search_btn)
    TextView searchBarSearchBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_user);
        ButterKnife.bind(this);
        initListener();
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(searchBarDelete, aVoid -> doClearEditText());
        RxViewHelper.getInstance().clicks(searchBarSearchBtn, aVoid -> doSearch());
        RxTextView.editorActionEvents(searchBarEditText).subscribe(textViewEditorActionEvent -> doSearch());

    }

    private void doSearch() {

        String str = searchBarEditText.getText().toString();
        if (TextUtils.isEmpty(str)) {
            ToastHelper.toast("请输入账号");
            return;
        }
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .searchFriend(AppUtils.getSignParams(currentUser.getMemberid(), str), currentUser.getMemberid(), str)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<UserEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<UserEntity> response) {
                        UserEntity userEntity = response.getInfo();
                        Intent intent = new Intent(SearchUserActivity.this, UserDetailActivity.class);
                        intent.putExtra("UserEntity", userEntity);
                        startActivity(intent);
                    }
                });
    }

    private void doClearEditText() {
        if (searchBarEditText.getText().toString() != null) {
            searchBarEditText.setText("");
        }
    }

}
